package com.coolled.ei.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coolled.ei.service.BaseService;
import com.coolled.ei.utils.LikeQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * ServiceImpl模板
 */
@Service
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

    @Override
    public List<T> getList(T temp) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>(temp);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<T> getLikeList(T temp) {
        LikeQueryWrapper<T> queryWrapper = new LikeQueryWrapper<>(temp);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public Long getCount(T temp) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>(temp);
        return this.baseMapper.selectCount(queryWrapper);
    }

    @Override
    public T getOne(T temp) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>(temp);
        return this.getOne(queryWrapper);
    }

    @Override
    public int add(T temp) throws Exception {
        int code = this.baseMapper.insert(temp);
        if (code == 0) {
            throw new Exception("新增失败");
        }
        return code;
    }

    @Override
    public int update(T temp) throws Exception {
        int code = this.baseMapper.updateById(temp);
        if (code == 0) {
            throw new Exception("修改失败");
        }
        return code;
    }

    @Override
    public int delete(Collection<?> ids) throws Exception {
        int code = this.baseMapper.deleteBatchIds(ids);
        if (code != ids.size()) {
            throw new Exception("删除失败");
        }
        return code;
    }

    @Override
    public int delete(Wrapper<T> queryWrapper) {
        return this.baseMapper.delete(queryWrapper);
    }
}
